package com.atguigu.gmall.product.controller;

import java.util.concurrent.*;

/**
 * ClassName: CompletableFutureDemo
 * Package: com.atguigu.gmall.product.controller
 * Description:
 *
 * @Author 刘轩
 * @Create 2023-08-25 18:30
 * @Version 1.0
 */


public class CompletableFutureDemo {
    public static void main(String[] args) throws ExecutionException,InterruptedException {
      /*  // 创建一个没有返回值
        CompletableFuture<Void> completableFuture = CompletableFuture.runAsync(() -> {
            System.out.println("没有返回值");
        });
        System.out.println(completableFuture.get());*/


        /*// 创建一个有返回值
        CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
            return 200;
        });
        integerCompletableFuture.whenComplete((t,u)->{
            System.out.println(t);
            System.out.println(u);
        });
        System.out.println(integerCompletableFuture.get());*/

       /* CompletableFuture<Integer> integerCompletableFuture = CompletableFuture.supplyAsync(() -> {
            return 200;
        }).thenApply(t->{
            System.out.println(t);
            return 2*t;
        });
        System.out.println(integerCompletableFuture.get());*/
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(
                3,
                6,
                3,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );
        CompletableFuture<String> completableFutureA = CompletableFuture.supplyAsync(() -> {
            return "hello";
        },threadPoolExecutor);
        CompletableFuture<Void> completableFutureB = completableFutureA.thenAcceptAsync(s -> {
            try {
                Thread.sleep(1000);

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("b = " + s);
        },threadPoolExecutor);
        CompletableFuture<Void> completableFutureC = completableFutureA.thenAcceptAsync(s -> {
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("c = " + s);
        },threadPoolExecutor );
        System.out.println(completableFutureB.get());
        System.out.println(completableFutureC.get());

    }
}
